<script setup lang="ts">
import TitlePanel from "@/views/ecg/doctor/components/TitlePanel.vue";
import DevInstallPanel from "@/views/ecg/doctor/components/DevInstallPanel.vue";
import QueuePanel from "@/views/ecg/doctor/components/QueuePanel.vue";
import {DoctorApi, PatientStatisticVO, RoomBedVO} from '@/api/ecg/doctor';
import {useUserStore} from "@/store/modules/user";
import {QueueVO} from "@/api/ecg/queue";
import {ElNotification} from "element-plus";
import {useRoomStore} from "@/store/modules/room";

const roomStore = useRoomStore();

const roomBedVO = ref<RoomBedVO>({
    roomId: null,
    roomName: null,
    bedNo: null,
    checkTypes: null,
    opType: null
})

const installingPatient = ref<QueueVO>({
  bedNo: "", bookCheckType: 0, bookTimeslot: 0, expired: 0, id: 0, jumpFlag: 0, passed: 0,
  patGender: 0, patId: "", patName: "", roomId: 0, roomName: "", seqNum: 0, status: 0
})

const patientStat = ref<PatientStatisticVO>({
    finishedNum: 0,
    readyNum: 0,
    receivedNum: 0,
    passedNum: 0,
    queuingNum: 0
})

const autoCall = ref<boolean>(false);
const state = ref<boolean>()

const patientList = ref<QueueVO[]>([])

let timerRunFlag : boolean = false
const bedControlFlag = ref<boolean>(true);
const finishFlag = ref<boolean>(true);
const passFlag = ref<boolean>(true);

const finishNextPatient = async () => {
  finishFlag.value = true
  try {
    patientList.value = await DoctorApi.finishInstallNextPatient(roomBedVO.value)
    patientStat.value = await DoctorApi.getDevInstallStatistic(roomBedVO.value)
    getInstallingPatient()

    // 用于解决 左侧重置后 获取当前 【安装中】患者
    setInstallingPatient(installingPatient.value)
  } finally {
    finishFlag.value = false
  }
}

const passNextPatient = async () => {
  passFlag.value = true
  try {
    patientList.value = await DoctorApi.passInstallNextPatient(roomBedVO.value)
    patientStat.value = await DoctorApi.getDevInstallStatistic(roomBedVO.value)
    getInstallingPatient()
  } finally {
    passFlag.value = false
  }
}

const callAgain = async () => {
  await DoctorApi.callInstallingPatientAgain(roomBedVO.value)
}

const getInstallingPatient = () => {
  const tempPatient : QueueVO | undefined = patientList.value.find(item => item.status === 36)
  if (undefined === tempPatient) {
    //setInstallingPatient(undefined)
  } else if (tempPatient.patId !== installingPatient.value.patId) {
    installingPatient.value= tempPatient
    setInstallingPatient(tempPatient)
  }
}

const initLoad = async () => {
  if (roomBedVO.value.roomId == null) {
    ElNotification.error({
      message: '请重新就座!',
      type: 'info',
      duration: 3000 // 自动关闭时间，默认为3000ms
    })
    return
  }

  patientList.value = await DoctorApi.getToBeInstalledPatientList(roomBedVO.value)
  getInstallingPatient()
  patientStat.value = await DoctorApi.getDevInstallStatistic(roomBedVO.value)
  const queueVO2 = await DoctorApi.bedDoctorGet(roomBedVO.value)
  if (queueVO2.status === 20)
    state.value = true
  else if (queueVO2.status === 30)
    state.value = false
}

const bedControl = async () => {
  bedControlFlag.value = true
  try {
    if (state.value) {
      await DoctorApi.bedDoctorPause(roomBedVO.value)
      state.value = false
    } else {
      await DoctorApi.bedDoctorResume(roomBedVO.value)
      state.value = true
    }
  } finally {
    bedControlFlag.value = false
  }
}

const doctorTimer = () => {
  console.log('doctorTimer ...')
  initLoad()
  if (timerRunFlag)
    setTimeout(doctorTimer, 5000) // five seconds
}

/** 初始化 **/
onMounted(() => {
  timerRunFlag = false
  bedControlFlag.value = true
  finishFlag.value = true
  passFlag.value = true

  if (roomStore.isSetRoom) {
    roomBedVO.value.roomId = roomStore.room!.roomId
    roomBedVO.value.roomName = roomStore.room!.roomName
    roomBedVO.value.bedNo = roomStore.room!.bedNo
    roomBedVO.value.checkTypes = roomStore.room!.checkTypes
    roomBedVO.value.opType = roomStore.room!.opType

    timerRunFlag = true
    doctorTimer()
  } else {
    roomBedVO.value.roomId = null
    roomBedVO.value.bedNo = null
  }

  bedControlFlag.value = false
  finishFlag.value = false
  passFlag.value = false

  console.info( roomBedVO.value )
})

onUnmounted( () =>{
  timerRunFlag = false
})

const onEventDevInstall = () => {
   //console.info("onDevInstallPanelEvent...")
  installingPatient.value.patId = ""

  if (autoCall.value) {
    finishNextPatient()
  } else {
    initLoad()
  }
}

const onEventRecallFinish = () => {
  initLoad()
}

/** 患者变更操作 */
const devInstallPanelRef = ref()
const setInstallingPatient = (tempPatient: QueueVO | undefined) => {
  devInstallPanelRef.value.setPatient(tempPatient)
}

</script>

<template>
  <el-container>
    <el-header style="background-color: var(--el-color-primary-light-7); font-size: 24px">
      <TitlePanel :titleType=2 :room="roomBedVO" :patientStatistic="patientStat" />
    </el-header>
    <el-container style="height: 100%;">
      <el-main style="border: solid var(--el-color-primary-light-7)">
        <DevInstallPanel :room="roomBedVO" ref="devInstallPanelRef" @event_dev_install="onEventDevInstall"/>
      </el-main>
      <el-aside width="400px" style="border: solid var(--el-color-primary-light-7)">
          <QueuePanel :room="roomBedVO" :queue="patientList" @event_RecallFinish="onEventRecallFinish"/>
      </el-aside>
    </el-container>
    <el-container class="right-bottom">
      <el-checkbox label="自动&nbsp;" v-model="autoCall" />
      <el-button :disabled="finishFlag" type="primary" @click="finishNextPatient">叫号</el-button>
      <el-button :disabled="passFlag" type="primary" @click="passNextPatient">过号</el-button>
      <el-button  type="primary" @click="callAgain">重叫</el-button>
      <el-button :disabled="bedControlFlag" type="primary" @click="bedControl">{{state?'暂停':'恢复'}}</el-button>
    </el-container>
  </el-container>
</template>

<style scoped lang="scss">
.right-bottom {
  position: absolute;
  bottom: 50px;
  right: 80px;
  //background-color: #f00;
  //color: #fff;
  //padding: 10px;
}
</style>
